import * as Sequelize from "sequelize";
import { Application } from "egg";

interface UserAttributes {
    id: string;
    /*org_id: string;
    role_id: string;*/
    account: string;
    name: string;
    password: string;
    email: string;
    mobile?: string;
    status: string;
  create_user_id: string;
  modify_user_id: string;
    create_time: string;
    modify_time: string;
}

type UserInstance = Sequelize.Instance<UserAttributes> & UserAttributes;

export default (app: Application) => {
    const { STRING, CHAR, UUID, UUIDV4, DATE,NOW } = app.Sequelize;
    const attributes: SequelizeAttributes<UserAttributes> = {
        id: { type: UUID, primaryKey: true, allowNull: false, defaultValue: UUIDV4 },
        /*org_id: { type: UUID, primaryKey: true, allowNull: false },
        role_id: { type: UUID, primaryKey: true, allowNull: false},*/
        account: { type: STRING(50), allowNull: false },
        name: { type: STRING(50), allowNull: false },
        password: { type: STRING(50), allowNull: false },
        email: { type: STRING(50), allowNull: false },
        mobile: { type: STRING(50), allowNull: true },
        status: { type: CHAR(3), allowNull: false, defaultValue: "000" },
      create_user_id: { type: UUID, allowNull: false },
      modify_user_id: { type: UUID, allowNull: false },
        create_time: { type: DATE, allowNull: false ,defaultValue: NOW},
        modify_time: { type: DATE, allowNull: false ,defaultValue: NOW}
    };
    const model = app.model.define<UserInstance, UserAttributes>("User", attributes,{
        tableName: 't_user'
    });
    model.associate = ()=>{
        app.model.models.User.hasOne(app.model.models.RoleUser,{foreignKey:"user_id"})
        app.model.models.User.hasOne(app.model.models.OrgUser,{foreignKey:"user_id"})
    };
    return model
};
